<template>
  <div class="potentialCustomers">
    <page-header :title="'用户管理'"></page-header>
    <div class="search_area">
      <el-row>
        <el-col :span="24">
          <div class="btn_area">
            <el-button class="confrim" @click="add">新增</el-button>
          </div>
        </el-col>
      </el-row>
    </div>
    <table-list :fields="fields" :list="tableDataRender" :pagination="pagination" :total="total" @sizeChange="handleSizeChange" @currentChange="handleCurrentChange" @handle="onHandle"></table-list>
    <el-dialog :title="code == '1' ? '新增用户': '编辑用户'" :visible.sync="visible" @close="close">
      <el-row>
        <el-form :form="userForm" label-width="1rem">
          <el-col :span="11">
            <el-form-item label="用户名" required>
              <el-input type="text" v-model="userForm.account"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="11">
            <el-form-item label="姓名" required>
              <el-input type="text" v-model="userForm.userName"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="11">
            <el-form-item label="邮箱" required>
              <el-input type="text" v-model="userForm.mail"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="11">
            <el-form-item label="状态" required>
              <el-radio v-model="userForm.status" label="1">启用</el-radio>
              <el-radio v-model="userForm.status" label="2">禁用</el-radio>
            </el-form-item>
          </el-col>
        </el-form>
      </el-row>
      <span slot="footer" class="dialog-footer">
        <el-button class="cancle" @click="visible = false">取 消</el-button>
        <el-button class="confrim" @click="confrim">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>
<script>
import tableList from '@/components/table.vue'
import pageHeader from '@/components/pageHeader.vue'
export default {
  name: 'potentialCustomers',
  components: {
    tableList,
    pageHeader
  },
  data() {
    return {
      queryForm: {
        name: '',
        area: '',
        type: ''
      },
      userForm: {
        account: "",
        mail: "",
        status: "1",
        userName: "",
      },
      code: '',
      list: [],
      fields: [
        {
          prop: 'rowNum',
          label: '序号',
          align: 'center'
        },
        {
          prop: 'userName',
          label: '姓名',
          align: 'center'
        },
        {
          prop: 'account',
          label: '用户名',
          align: 'center'
        },
        {
          prop: 'mail',
          label: '邮箱',
          align: 'center'
        },
        {
          prop: 'createTime',
          label: '创建时间',
          align: 'center'
        },
        {
          prop: 'statusName',
          label: '状态',
          align: 'center'
        },
        {
          prop: 'oper',
          label: '操作',
          align: 'center',
          btns: [
            {type: 'update',desc:'编辑'}
          ]
        }
      ],
      pagination: {
        currentPage: 1,
        pageSize: 10
      },
      total: 100,
      tableDataRender: [],
      visible: false
    }
  },
  methods: {
    add() {
      this.code = '1';
      this.visible = true;
    },
    close() {
      this.userForm = {
        account: "",
        mail: "",
        status: "1",
        userName: "",
      }
    },
    confrim() {
      let url = this.code == '1' ? this.$api.user.add : this.$api.user.update;
      this.$post(url,this.reqData(this.userForm)).then(res => {
        if (res.data.code == 200) {
          this.visible = false;
          if (this.code == '1') {
            this.$message({
              showClose: true,
              message: res.data.message + ',' + '用户初始密码为：123456',
              type: 'success',
              duration: 0
            });
          } else {
            this.$message.success(res.data.message);
          }
          this.queryUserList();
        } else {
          this.$message.error(res.data.message);
        }
      })
    },
    onHandle(data) {
      let {row,type} = data;
      this.code = '2';
      this.userForm.account = row.account,
      this.userForm.mail = row.mail,
      this.userForm.status = row.status,
      this.userForm.userName = row.userName,
      this.userForm.uuid = row.uuid,
      this.userForm.id = row.id,
      this.visible = true;
      console.log(row,type)
    },
    queryUserList:function() {
      let url = this.$api.user.list;
      this.$post(url).then(res => {
        if (res.data.code == 200) {
          this.list = res.data.data;
          this.list.forEach((item,index) => {
            item.rowNum = index + 1
            item.statusName = '1' ? '启用' : '禁用'
          })
          this.tableDataRender = this.pageMethod()
          this.total = res.data.data.length;
        } else {
          this.$message.error(res.data.message)
        }
      })
    },
    handleSizeChange(val) {
      console.log(val);
      this.pagination.pageSize = val;
      this.tableDataRender = this.pageMethod()
    },
    handleCurrentChange(val) {
      console.log(val);
      this.pagination.currentPage = val;
      this.tableDataRender = this.pageMethod()
    },
    pageMethod() {
      let result = []
      let start = (this.pagination.currentPage - 1) * this.pagination.pageSize;
      let end = this.pagination.currentPage * this.pagination.pageSize - 1;
      for (let i = start; i <= end; i++) {
        if (this.list[i]) {
          result.push(this.list[i])
        }
      }
      return result
    }
  },
  mounted() {
    this.queryUserList();
  }
}
</script>
<style scoped lang="less">
.potentialCustomers {
  background: #fff;
  overflow: hidden;
  padding-bottom: 0.15rem;
  .search_area .btn_area {
    text-align: right;
  }
}
</style>